package com.tianshi.user.service.impl;

import com.tianshi.entity.Users;
import com.tianshi.user.mapper.UserMapper;
import com.tianshi.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    //登录
    @Override
    public Users login(String keyword, String password) {
        //查询用户是否存在
        Users user = userMapper.selectByKeyword(keyword);
        if(user!= null && user.getPassword().equals(password)){
            return user;
        }
        return null;
    }

    //注册
    @Override
    public void register(Users user) {
        //设置创建时间
        user.setCreatedAt(LocalDateTime.now());
        user.setUserType((short)0); //默认是普通用户
        userMapper.insert(user);
    }
    //根据手机号查询用户
    @Override
    public Users findByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }

    @Override
    public Users findUserById(Long userId) {
        return userMapper.selectByUserId(userId);
    }

    @Override
    public void updateUserInfo(Users user) {
        userMapper.updateUserInfo(user);
    }

    @Override
    public void changePassword(Long userId, String oldPassword, String newPassword) {
//        userMapper.changerPassword(userId, oldPassword, newPassword);
    }

}
